<template>
    <el-dialog
            :title="!dataForm.customerId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID"></el-input>
                </el-form-item>
                <el-form-item label="客户名称" prop="customerName">
                        <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
                </el-form-item>
                <el-form-item label="信用额度" prop="creditLimit">
                        <el-input v-model="dataForm.creditLimit" placeholder="信用额度"></el-input>
                </el-form-item>
                <el-form-item label="信用期限（天）" prop="creditTerm">
                        <el-input v-model.number="dataForm.creditTerm" placeholder="信用期限（天）"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contactPerson">
                        <el-input v-model="dataForm.contactPerson" placeholder="联系人"></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="phone">
                        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
                </el-form-item>
                <el-form-item label="电子邮箱" prop="email">
                        <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            customerId: null,
            customerName: null,
            creditLimit: null,
            creditTerm: null,
            contactPerson: null,
            phone: null,
            email: null
          },
          dataRule: {
            customerId: [
                        { required: true, message: '客户ID不能为空', trigger: 'blur' }
            ],
            customerName: [
                        { required: true, message: '客户名称不能为空', trigger: 'blur' }
            ],
            creditLimit: [
                        { required: true, message: '信用额度不能为空', trigger: 'blur' }
            ],
            creditTerm: [
                        { required: true, message: '信用期限（天）不能为空', trigger: 'blur' }
            ],
            contactPerson: [
                        { required: true, message: '联系人不能为空', trigger: 'blur' }
            ],
            phone: [
                        { required: true, message: '联系电话不能为空', trigger: 'blur' }
            ],
            email: [
                        { required: true, message: '电子邮箱不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.customerId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.customerId = id || 0
            if (this.dataForm.customerId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customers/info/${this.dataForm.customerId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomers
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customers/${!this.dataForm.customerId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>